package com.dy.details.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.details.service.ShoppingCarService;
import com.dy.pojo.Orders;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kaoshi.common.config.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/shoppringCar")
@RestController
@CrossOrigin
public class ShoppringCarController {
    @Autowired
    private ShoppingCarService shoppingCarService;

    @RequestMapping("findall")
    public Result findall(){
        List<Orders> orders= shoppingCarService.findall();
        return Result.ok(orders);
    }


    @GetMapping("getOrderId")
    public Result getOrderId() {
        // 假设我们从数据库中获取最新的订单ID
        Orders latestOrder = shoppingCarService.getOne(new QueryWrapper<Orders>().orderByDesc("id").last("limit 1"));
        if (latestOrder != null) {
            return Result.ok(latestOrder.getId());
        } else {
            return Result.fail("No orders found");
        }
    }
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        shoppingCarService.removeById(id);
        return Result.ok();
    }

    @RequestMapping("update")
    public Result update(@RequestBody Orders orders){
        shoppingCarService.updatestatus(orders);
        return Result.ok();
    }
}
